#include "ULUI_LookupSingle.h"
#include "UL_Global.h"
#include "ULGfx_History.h"

#define SINGLE_LOOKUP_START_X	100
#define SINGLE_LOOKUP_START_Y	100

ULUI_LookupSingle ULUI_LookupSingle::singleLookup;

enum HistoryLookupIDs{
	SERIAL_NUMBER_BOX,
	DATE_BOX,
	BALL_BOX,
	OK,
	EXIT	
};	

static char *menuTitles[] = {
	"                                ",
	"                                ",
	"                                ",
	"确定",
	"返回",
};

#define FIXED_GAP	20

void ULUI_LookupSingle::Init()
{
	int i;
	ULUI_Menu menu;

	AttachEventHandler(this);

	pad.Reset();
	pad.EnableSolidBg(true);
	menu.SetFont(UL_Global::UnionLottoFont());
	pad.SetFont(UL_Global::UnionLottoFont());
	selectedMenu = -1;

	for (i = 0; i <= EXIT; i++){
		menu.SetID(i);
		menu.SetTitle(menuTitles[i]);
		menus.AddMenu(menu);
	}

	//Find menu's X is after tip
	menu.SetTitle("                                ");
	menu.GetTitleSize(tipWidth, tipHeight);
	menus.SetFirstMenuXY(VSDL_Point(SINGLE_LOOKUP_START_X + tipWidth, SINGLE_LOOKUP_START_Y + tipHeight));
	pad.SetStartXY(VSDL_Point(SINGLE_LOOKUP_START_X + tipWidth * 2 + FIXED_GAP, SINGLE_LOOKUP_START_Y));
	selectedBox = SERIAL_NUMBER_BOX;
}

void ULUI_LookupSingle::CleanUp()
{
	pad.Reset();
	menus.Reset();
}

void ULUI_LookupSingle::Pause()
{
}

void ULUI_LookupSingle::Resume()
{
	selectedMenu = -1;
}

void ULUI_LookupSingle::Update(VSDL_GameEngine *game)
{
	if (selectedMenu == -1){
		return ;
	}

	switch (selectedMenu){
		case SERIAL_NUMBER_BOX:
			if (selectedBox != SERIAL_NUMBER_BOX){
				selectedBox = SERIAL_NUMBER_BOX;
				pad.ClearString();
			}
			break;
		case DATE_BOX:
			if (selectedBox != DATE_BOX){
				selectedBox = DATE_BOX;
				pad.ClearString();
			}
			break;
		case BALL_BOX:
			if (selectedBox != BALL_BOX){
				selectedBox = BALL_BOX;
				pad.ClearString();
			}
			break;
		case OK:
			if (pad.GetString() != NULL){
				inputBoxString = pad.GetString();
			}else{
				inputBoxString.clear();
			}
			break;
		case EXIT:
			game->PopState();
			break;
	}
}

void ULUI_LookupSingle::Draw(VSDL_GameEngine *game)
{
	VSDL_RGB rgbNormal(100, 100, 100);
	VSDL_RGB rgbSelected(250, 100, 100);
	VSDL_RGB rgb;
	char *str;
	VSDL_Point currentXY(SINGLE_LOOKUP_START_X, SINGLE_LOOKUP_START_Y);
	SDL_Surface *destSurf = game->GetSurface();
	ULG_History *ulgHistory = ULG_History::Instance();;
	int serialNo;
	int historyY = SINGLE_LOOKUP_START_Y + (EXIT + 2) * tipHeight;


	ulgHistory->SetStartXY(VSDL_Point(SINGLE_LOOKUP_START_X, historyY));

	UL_Global::UnionLottoShowBg();
	UL_Global::UnionLottoShowTitle();
	menus.Show(destSurf);
	//Draw history data background
	SPG_RectFilled(destSurf, SINGLE_LOOKUP_START_X, historyY, destSurf->w, destSurf->h, rgbNormal.GetRGB32(destSurf));
	//Show tips
	UL_Global::UnionLottoShowTip("输入任意一项进行查询", currentXY);
	currentXY.y += tipHeight;
	UL_Global::UnionLottoShowTip("期号查询:(例:2012001)", currentXY);
	
	str = pad.GetString();

	if (selectedBox == SERIAL_NUMBER_BOX){
		rgb = rgbSelected;

		if (str != NULL){
			UL_Global::UnionLottoShowTip(str, VSDL_Point(currentXY.x + tipWidth, currentXY.y));
		}

		if (inputBoxString.length() != 0){
			serialNo = strtoul(inputBoxString.c_str(), NULL, 10);
			ulgHistory->ShowSerial(destSurf, serialNo);
		}
	}else{
		rgb = rgbNormal;
	}

	SPG_Rect(destSurf, currentXY.x + tipWidth, currentXY.y, currentXY.x + tipWidth * 2, currentXY.y + tipHeight, rgb.GetRGB32(destSurf));
	rgb = rgbNormal;

	currentXY.y += tipHeight;
	UL_Global::UnionLottoShowTip("日期查询:(例:2012-01-01)", currentXY);

	if (selectedBox == DATE_BOX){
		rgb = rgbSelected;
		
		if (str != NULL){
			UL_Global::UnionLottoShowTip(str, VSDL_Point(currentXY.x + tipWidth, currentXY.y));
		}

		if (inputBoxString.length() != 0){
			ulgHistory->ShowSerial(destSurf, inputBoxString.c_str());
		}
	}else{
		rgb = rgbNormal;
	}

	SPG_Rect(destSurf, currentXY.x + tipWidth, currentXY.y, currentXY.x + tipWidth * 2, currentXY.y + tipHeight, rgb.GetRGB32(destSurf));
	rgb = rgbNormal;


	currentXY.y += tipHeight;
	UL_Global::UnionLottoShowTip("球号查询:(例:02-..-13)", currentXY);

	if (selectedBox == BALL_BOX){
		rgb = rgbSelected;
		
		if (str != NULL){
			UL_Global::UnionLottoShowTip(str, VSDL_Point(currentXY.x + tipWidth, currentXY.y));
		}
		
		if (inputBoxString.length() != 0){
			int n[7];
			ULC_Balls balls;
			sscanf(inputBoxString.c_str(), "%d-%d-%d-%d-%d-%d-%d", n, n + 1, n + 2, n + 3, n + 4, n + 5, n + 6);
			balls.SetNumbers(n, n[6]);

			ulgHistory->ShowSerial(destSurf, balls);
		}	
	}else{
		rgb = rgbNormal;
	}

	SPG_Rect(destSurf, currentXY.x + tipWidth, currentXY.y, currentXY.x + tipWidth * 2, currentXY.y + tipHeight, rgb.GetRGB32(destSurf));

	//Draw input pad
	pad.Show(destSurf);
	//Draw spliting line
	historyY--;
	SPG_Line(destSurf, currentXY.x, historyY, destSurf->w, historyY, rgbNormal.GetRGB32(destSurf));
}

ULUI_LookupSingle *ULUI_LookupSingle::Instance()
{
	return &singleLookup;
}


void __HANDLE_EVENT ULUI_LookupSingle::OnKeyDown(SDLKey keysym, unsigned char *keyStates, void *arg)
{
	VSDL_GameEngine *game = reinterpret_cast<VSDL_GameEngine *>(arg);

	//Basic
	if (keyStates[SDLK_ESCAPE]){
		game->PopState();
	}

	//Special cases
	if (keyStates[SDLK_RETURN]){
		selectedMenu = OK;
		return ;
	}

	if (keyStates[SDLK_UP]){
		selectedMenu--;

		if (selectedMenu < 0){
			selectedMenu = 0;
		}
		
		return ;
	}

	if (keyStates[SDLK_DOWN]){
		selectedMenu++;

		if (selectedMenu > OK){
			selectedMenu = OK;
		}

		return ;
	}

	pad.PushKeysym(keysym);
}

void __HANDLE_EVENT ULUI_LookupSingle::OnMouseMove(int button, unsigned char buttonState, int absX, int absY, int relX, int relY, void *arg)
{
	menus.UpdateMenuList(absX, absY);
	pad.UpdateStatus(absX, absY);
}

void __HANDLE_EVENT ULUI_LookupSingle::OnMouseDown(int button, unsigned char buttonState, int absX, int absY, void *arg)
{
	int i;
	VSDL_GameEngine *game = reinterpret_cast<VSDL_GameEngine *>(arg);

	selectedMenu = -1;

	for (i = 0; i <= EXIT; i++){
		ULUI_Menu &menu = menus.GetMenu(i);

		if (menu.IsXYInRect(absX, absY)){
			selectedMenu = i;
			break;
		}
	}

	pad.OnMouseDown(absX, absY);
}

void __HANDLE_EVENT ULUI_LookupSingle::OnQuit(void *arg)
{
}
